# -*- coding: utf-8 -*-
"""
========================================================================================================================
@project : AosSanic
@file: OAClient
@Author: mengying
@email: 652044581@qq.com
@date: 2022/4/3 10:20
@desc: O2OA的个人相关接口
========================================================================================================================
"""

from OAClient import O2OAClient
from config import config
from OAError import O2OAError
import requests, json


class O2OAPerson(O2OAClient):
    """020A个人相关"""

    def __init__(self):
        super(O2OAPerson, self).__init__()

    def create(self, name, phone, sex, unique):
        """
        @desc: 新建人员
        @params: name 人员的名字
        @params： phone 人员的电话
        @params: sex 人员的性别
        @params: unique 人员的唯一编码
        注： 里面还有很多参数，如邮箱这些，不是必填就没写，可以在此拓展，sex 枚举 m 男， f 女
        """

        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person"
        data = {
            "genderType": sex,
            "signature": "",
            "description": "",
            "unique": unique,
            "orderNumber": "",
            "superior": "",
            "officePhone": "",
            "boardDate": "",
            "birthday": "",
            "employee": "",
            "password": "",
            "display": "",
            "qq": "",
            "mail": "",
            "weixin": "",
            "weibo": "",
            "mobile": phone,
            "name": name,
            "ipAddress": "",
            "controllerList": [],
            "woPersonAttributeList": [],
            "woIdentityList": [],
            "control": {"allowEdit": True, "allowDelete": True}
        }

        response = requests.post(url=url, headers=self.o2oa_headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']["id"]

    def delete(self, person_id):
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person/" + person_id
        response = requests.delete(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']

    def read(self, person_id):
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person/" + person_id
        response = requests.get(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']

    def list(self):
        """
        @desc: 获取人员列表
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person/list/(0)/next/10"
        response = requests.get(url=url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        count = response['count']
        # 如果比17就不发第二次
        if count <= 10:
            return response['data']

        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person/list/(0)/next/{}".format(str(count))
        response = requests.get(url=url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))
        return response['data']

    def reset_password(self, user_id):
        """
        @desc: 重置密码
        @params: user_id 人员的id 创建的时候生成
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/person/" + user_id + "/reset/password"
        response = requests.get(url=url, headers=self.o2oa_headers).json()

        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']

    def modify_password(self, oldPassword, newPassword, token):
        """
        @desc: 修改密码
        @params: user_id 人员的id 创建的时候生成，不能用默认的用户获取token
        """
        data = {
            "oldPassword": oldPassword,
            "newPassword": newPassword,
            "confirmPassword": newPassword
        }
        headers = {"x-token": token}
        url = config.O2OA_URL + "/x_organization_assemble_personal/jaxrs/person/password"
        response = requests.put(url=url, headers=headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']


if __name__ == '__main__':
    # 创建人员
    # print(O2OAPerson().create(name="孟颖", phone="17783098372", sex="m", unique="x1231232"))

    # 查询列表
    # print(O2OAPerson().list())

    # 删除人员
    # print(O2OAPerson().delete('017b6ede-b8e5-4e5e-b7bc-8d2e34df0a58'))

    # 查询个人信息
    print(O2OAPerson().read('a21cbff4-51fb-42fc-8510-cac7e8ccf467'))
